<?php
/**
 * Created by PhpStorm.
 * User: nightelf
 * Date: 2020/1/13
 * Time: 15:30
 */

namespace App\Http\Model;


use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class GroupModel extends Model
{
    const TABLE = 'group';
    private $startTime = '';
    private $endTime = '';

    public function __construct()
    {
        $yearInfo = (new YearModel())->findYear(date('Y-m-d H:i:s'));
        if (!empty($yearInfo)){
            $this->startTime = $yearInfo['startTime'];
            $this->endTime = $yearInfo['endTime'];
        }else{
            $this->startTime = date('Y-01-01 00:00:00');
            $this->endTime = date('Y-12-31 23:59:59');
        }
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function lists(array $data)
    {
        $result = DB::table(self::TABLE)
            ->leftJoin('profession as p', 'p.id', '=', self::TABLE . '.professionId')
            ->leftJoin('project', 'project.id', '=', self::TABLE . '.projectId')
            ->where(function ($query) use ($data) {
                if (!empty($data['status'])) {
                    $query->where(self::TABLE . '.status', $data['status']);
                }
                if (!empty($data['projectId']) && $data['projectId'] != 0) {
                    $query->where(self::TABLE . '.projectId', $data['projectId']);
                }
                if (!empty($this->startTime) && !empty($this->endTime)) {
                    $query->whereBetween(self::TABLE . '.createTime', [$this->startTime, $this->endTime]);
                }
                if (isset($data['professionId']) && !is_null($data['professionId'])) {
                    $query->where(self::TABLE . '.professionId', $data['professionId']);
                }
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(self::TABLE . '.name', 'like', '%' . $data['search'] . '%');
                }
            })
            ->select(self::TABLE . '.*', 'p.name as professionName', 'project.name as projectName');
        if (!empty($data['length']) && !empty($data['page'])) {
            $limit = config('yucheng.limit');
            $start = 0;
            if (!empty($data['length'])) {
                $limit = $data['length'];
            }
            if (!empty($data['page'])) {
                $start = ($data['page'] - 1) * $limit;
            }
            $result = $result->offset($start)->limit($limit);
        }

        return $result->get()->toArray();
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function countLists(array $data)
    {
        return DB::table(self::TABLE)
            ->leftJoin('profession as p', 'p.id', '=', self::TABLE . '.professionId')
            ->leftJoin('project', 'project.id', '=', self::TABLE . '.projectId')
            ->where(function ($query) use ($data) {
                if (!empty($data['status'])) {
                    $query->where(self::TABLE . '.status', $data['status']);
                }
                if (!empty($data['projectId'])) {
                    $query->where(self::TABLE . '.projectId', $data['projectId']);
                }
                if (!empty($this->startTime) && !empty($this->endTime)) {
                    $query->whereBetween(self::TABLE . '.createTime', [$this->startTime, $this->endTime]);
                }
                if (isset($data['professionId']) && !is_null($data['professionId'])) {
                    $query->where(self::TABLE . '.professionId', $data['professionId']);
                }
                if (isset($data['search']) && !is_null($data['search'])) {
                    $query->where(self::TABLE . '.name', 'like', '%' . $data['search'] . '%');
                }
            })
            ->count();
    }

    /**
     * 检查是否有重复
     * @param array $check
     * @param $id
     * @return array
     */
    public function checkRepeat(array $check, $id = 0)
    {
        $result = DB::table(self::TABLE)->where($check)
            ->where(function ($query) use ($id) {
                if ($id != 0) {
                    $query->where('id', '!=', $id);
                }
                if (!empty($this->startTime) && !empty($this->endTime)) {
                    $query->whereBetween(self::TABLE . '.createTime', [$this->startTime, $this->endTime]);
                }
            })
            ->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $data
     * @return array
     */
    public function info(array $data)
    {
        $result = DB::table(self::TABLE)->where($data)->first();
        return empty($result) ? [] : get_object_vars($result);
    }

    /**
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function update(array $where, array $data)
    {
        return DB::table(self::TABLE)->where($where)->update($data);
    }

    /**
     * @param array $data
     * @return
     */
    public function delete(array $data)
    {
        return DB::table(self::TABLE)->where($data)->delete();
    }

    /**
     * 整班组调整人员所在项目
     * @param array $param
     * @return bool
     */
    public function changeProject(array $param)
    {
        try {
            DB::transaction(function () use ($param) {
                //组员调动至另一个项目
                $groupMembersModel = new GroupMembersModel();
                $memberLists = $groupMembersModel->lists(['groupId' => $param['id'],'status'=>1]);
                $ids = [];
                foreach ($memberLists as $l) {
                    array_push($ids, $l->employeeId);
                }

                $this->update(['id' => $param['id']], ['projectId' => $param['projectId']]);
                $employeeModel = new EmployeeModel();
                foreach ($ids as $id) {
                    $employeeModel->update(['id' => $id], ['projectId' => $param['projectId']]);
                }
            });
            return true;
        } catch (QueryException $e) {
            $e->getBindings();
            return false;
        }
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function insert(array $data)
    {
        $data['createTime'] = date("Y-m-d H:i:s");
        return DB::table(self::TABLE)->insert($data);
    }
}